package com.lanyou.esb.web;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.JsonObject;
import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.Field;
import com.lanyou.cook.entity.domain.userManagement.User;
import com.lanyou.esb.entity.ExceptionErrorCodeSetting;
import com.lanyou.esb.repository.ExceptionErrorCodeSettingRepository;
import com.lanyou.esb.web.param.ExceptionErrorCodeSettingParam;
/**
 * 
 * @author ly-zengzugen
 *
 */


@RestController
@RequestMapping(Constants.BASE_URL_API + "/exceptionErrorCodeSettings")
public class ExceptionErrorCodeSettingController {

	@Autowired
	private ExceptionErrorCodeSettingRepository exceptionErrorCodeSettingRepository;
	/**
	 * 异常错误码设置
	 * @param param 类型:String 
	 * @param pageable 类型:String
	 * @return
	 */
	@RequestMapping("/search/findByExample")
	public Page<ExceptionErrorCodeSetting> findByExample(ExceptionErrorCodeSettingParam param, Pageable pageable) {
		
		Specification<ExceptionErrorCodeSetting> spec = new Specification<ExceptionErrorCodeSetting>() {
			@Override
			public Predicate toPredicate(Root<ExceptionErrorCodeSetting> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicatesList = new ArrayList<>();
				if(StringUtils.isNotBlank(param.getErrorCode())){
					predicatesList.add(cb.like(root.get("errorCode"), '%' + param.getErrorCode() + '%'));
				}
				
				if(StringUtils.isNotBlank(param.getErrorDesc())){
					predicatesList.add(cb.like(root.get("errorDesc"), '%' + param.getErrorDesc() + '%'));
				}
				
				query.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
				return null;
			}
		};
		return exceptionErrorCodeSettingRepository.findAll(spec, pageable);
	}
	//检查是否可以新增
	@RequestMapping(value = "/search/checkIsCanSave", produces = "application/json")
	public String checkIsCanSave(String errorCode,String originalId) {
		ExceptionErrorCodeSetting exceptionErrorCodeSetting = exceptionErrorCodeSettingRepository.findByErrorCode(errorCode);
			if (exceptionErrorCodeSetting != null) {
			if (!StringUtils.equals(exceptionErrorCodeSetting.getId(), originalId)) {
				JsonObject result = new JsonObject();
				result.addProperty("isCanSave", false);
				result.addProperty("errorCode", "DUPLICATE");
				return result.toString();
			}
		}

		JsonObject result = new JsonObject();
		result.addProperty("isCanSave", true);
		return result.toString();
	}
}